package com.xuyuan.socket.mycase1;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final MessageHandler chatMessageHandler;
    private final MessageHandler notificationMessageHandler;

    public WebSocketConfig(ChatMessageHandler chatMessageHandler, NotificationMessageHandler notificationMessageHandler) {
        this.chatMessageHandler = chatMessageHandler;
        this.notificationMessageHandler = notificationMessageHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new StrategyWebSocketHandler(chatMessageHandler), "/chat").setAllowedOrigins("*");
        registry.addHandler(new StrategyWebSocketHandler(notificationMessageHandler), "/notification").setAllowedOrigins("*");
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 消息缓冲区大小的大小
        container.setMaxTextMessageBufferSize(8192);
        container.setMaxBinaryMessageBufferSize(8192);
        return container;
    }
}